package dabs.engine.plant;

import java.io.Serializable;
import java.util.ArrayList;
import dabs.engine.messaging.NotificationHandler;

public class PowerPlant implements Serializable {
	private static final long serialVersionUID = 6391416797464333275L;

	private int powerGenerated;
	
	NotificationHandler poffice;
	
	private ArrayList<ComponentPart> components;
	
	public PowerPlant(NotificationHandler poffice) {
		this.poffice = poffice;
		
		components = new ArrayList<ComponentPart>();
		components.add(new Reactor(poffice, "reactor1", 1)); 			//0
		components.add(new Reactor(poffice, "reactor2", 0.5));			//1
		components.add(new Condenser(poffice, "condenser1", 1));		//2
		components.add(new Condenser(poffice, "condenser2", 0.7));		//3
		components.add(new Pump(poffice, "pump1"));						//4
		components.add(new Pump(poffice, "pump2"));						//5
		components.add(new Valve(poffice, "valve1", true));				//6
		components.add(new Valve(poffice, "valve2", true));				//7
		components.add(new Valve(poffice, "valve3", true));				//8
		components.add(new Valve(poffice, "valve4", true));				//9
		components.add(new Valve(poffice, "valve5", true));				//10
		components.add(new Valve(poffice, "valve6", true));				//11
		components.add(new Turbine(poffice, "turbine"));				//12
		
		components.get(0).setOutputs(components.get(12));
		components.get(1).setOutputs(components.get(12));
		
		components.get(12).setOutputs(components.get(6));
		components.get(12).setOutputs(components.get(7));
		
		components.get(6).setOutputs(components.get(2));
		components.get(7).setOutputs(components.get(3));
		
		components.get(2).setOutputs(components.get(4));
		components.get(3).setOutputs(components.get(5));
		
		components.get(4).setOutputs(components.get(8));
		components.get(4).setOutputs(components.get(9));
		
		components.get(5).setOutputs(components.get(10));
		components.get(5).setOutputs(components.get(11));
		
		components.get(8).setOutputs(components.get(0));
		components.get(9).setOutputs(components.get(1));
		components.get(10).setOutputs(components.get(0));
		components.get(11).setOutputs(components.get(1));
		
		components.get(0).getNewHydroState().receive(70, true);
		components.get(1).getNewHydroState().receive(70, true);
	}
	
	public int getPowerGenerated() { return powerGenerated; }
	public ArrayList<ComponentPart> getComponents() { return components; }
	public NotificationHandler getPostOffice() { return poffice; }
	
	public void tick(){
		for(ComponentPart part : components){
			part.tick();
		}
	}
	
	public void print() {
		for(ComponentPart part : components) {
			part.print();
		}
	}
	
	public ComponentPart getComponent(String name) throws Exception {
		for(ComponentPart part : components) {
			if (part.getName().equals(name)) return part;
		}
		throw new Exception("Component name not found");
	}

	public String getNameList() {
		String result = "";
		for (ComponentPart part : components) {
			result += part.getName() + " ";
		}
		return result;
	}
}
